package com.yuzhi.master.sewage.pipe.controller;

import cn.dev33.satoken.annotation.SaCheckPermission;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.yuzhi.common.annotation.Log;
import com.yuzhi.common.annotation.RepeatSubmit;
import com.yuzhi.common.core.controller.BaseController;
import com.yuzhi.common.core.domain.ApiResult;
import com.yuzhi.common.core.page.PageDataInfo;
import com.yuzhi.common.core.validate.AddGroup;
import com.yuzhi.common.core.validate.EditGroup;
import com.yuzhi.common.enums.BusinessTypeEnum;
import com.yuzhi.master.sewage.pipe.domain.DrainPipe;
import com.yuzhi.master.sewage.pipe.domain.bo.DrainPipeBo;
import com.yuzhi.master.sewage.pipe.domain.request.DrainPipeReq;
import com.yuzhi.master.sewage.pipe.domain.vo.DrainPipeVo;
import com.yuzhi.master.sewage.pipe.service.DrainPipeService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.validation.constraints.NotEmpty;
import javax.validation.constraints.NotNull;
import java.util.Arrays;
import java.util.List;
import java.util.Map;

@Validated
@RequiredArgsConstructor
@RestController
@RequestMapping("/drainpipe")
@Tag(name = "排水管道")
public class DrainPipeController extends BaseController {

    @Autowired
    private DrainPipeService drainPipeService;


    //查询管道总里程
    @Operation(summary = "查询管道总里程", description = "查询管道总里程")
    @GetMapping("/gdzlc")
    public Double getTotalLength() {
        return drainPipeService.getgdzlc();
    }

    //分区查询污水管网总里程
    @Operation(summary = "分区查询污水管网总里程", description = "分区查询污水管网总里程")
    @GetMapping("/lengthBydistrict")
    public ApiResult<List<DrainPipeVo>> getPipeLengthByDistrict() {
        List<DrainPipeVo> result = drainPipeService.getPipeLengthByDistrict();
        return ApiResult.ok(result);
    }

    /**
     * 查询管道信息列表
     */
    @Operation(
            summary = "查询管道信息列表",
            description = "查询管道信息列表"
    )
    @SaCheckPermission("master:drainPipe:list")
    @PostMapping("/list")
    public ApiResult<PageDataInfo> list(@RequestBody DrainPipeReq request) {
        return ApiResult.ok(drainPipeService.queryPageList(request));
    }

    /**
     * 分页查询数据
     */
    @Operation(summary = "分页查询数据", description = "分页查询数据")
    @GetMapping("/page")
    public IPage<DrainPipe> page(
            @RequestParam(defaultValue = "1") Integer pageNum,
            @RequestParam(defaultValue = "10") Integer pageSize,
            @RequestParam(required = false) String draiPipeNameRoad,
            @RequestParam(required = false) String draiPipeD1,
            @RequestParam(required = false) String draiPipeMaterial) {

        Page<DrainPipe> page = new Page<>(pageNum, pageSize);
        QueryWrapper<DrainPipe> queryWrapper = new QueryWrapper<>();
        if (draiPipeNameRoad != null && !draiPipeNameRoad.isEmpty()) {
            queryWrapper.like("drai_pipe_name_road", draiPipeNameRoad);
        }
        if (draiPipeD1 != null && !draiPipeD1.isEmpty()) {
            queryWrapper.ge("drai_pipe_d1", draiPipeD1);
        }
        if (draiPipeMaterial != null && !draiPipeMaterial.isEmpty()) {
            queryWrapper.le("drai_pipe_material", draiPipeMaterial);
        }
        return drainPipeService.page(page, queryWrapper);
    }

    /**
     * 获取管道信息详细信息
     */
    @Operation(summary = "获取管道信息详细信息", description = "获取管道信息详细信息")
    @SaCheckPermission("master:drainPipe:query")
    @GetMapping("/getInfo/{id}")
    public ApiResult<DrainPipeVo> getInfo(@NotNull(message = "主键不能为空") @PathVariable String id) {
        DrainPipeVo info = drainPipeService.queryById(id);
        return ApiResult.ok(info);
    }

    /**
     * 新增管道信息
     */
    @SaCheckPermission("master:drainPipe:add")
    @Log(title = "管道信息", businessType = BusinessTypeEnum.INSERT)
    @RepeatSubmit()
    @PostMapping("/add")
    public ApiResult<Void> add(@Validated(AddGroup.class) @RequestBody DrainPipeBo bo) {
        bo.setId(bo.getDraiPipeId());
        return toAjax(drainPipeService.insertByBo(bo));
    }


    /**
     * 修改管道信息
     */
    @SaCheckPermission("master:drainPipe:edit")
    @Log(title = "管道信息", businessType = BusinessTypeEnum.UPDATE)
    @RepeatSubmit()
    @PostMapping("/edit")
    public ApiResult<Void> edit(@Validated(EditGroup.class) @RequestBody DrainPipeBo bo) {
        return toAjax(drainPipeService.updateByBo(bo));
    }

    /**
     * 批量删除管道信息
     */
    @SaCheckPermission("master:drainPipe:remove")
    @Log(title = "管道信息", businessType = BusinessTypeEnum.DELETE)
    @DeleteMapping("/remove/{ids}")
    public ApiResult<Void> remove(@NotEmpty(message = "主键不能为空") @PathVariable String[] ids) {
        return toAjax(drainPipeService.deleteWithValidByIds(Arrays.asList(ids), true));
    }

    // 根据行政区划查询
    @Operation(summary = "根据行政区划查询", description = "根据行政区划查询")
    @GetMapping("/by-area")
    public List<DrainPipe> getByAreaCode(@RequestParam String areaCode) {
        QueryWrapper<DrainPipe> wrapper = new QueryWrapper<>();
        wrapper.eq("area_code", areaCode);
        return drainPipeService.list(wrapper);
    }

    /**
     * 获取污水管网采集数据列表
     */
    @Operation(summary = "管道监测数据列表", description = "管道监测数据列表")
    //@SaCheckPermission("master:drainPipe:query")
    @GetMapping("/getDrainDataList")
    public ApiResult<List<Map<String, Object>>> getDrainDataList() {
        return ApiResult.ok(drainPipeService.getDrainMonitorData());
    }

    @Operation(summary = "管道排水井关联信息", description = "管道排水井关联信息")
    @GetMapping("/getPipeManholeAltGrd")
    public ApiResult<List<Map<String, Object>>> getPipeManholeAltGrd(String factId) {
        return ApiResult.ok(drainPipeService.getPipeManholeAltGrd(factId));
    }


    /**
     * 管道统计（管径、管道材质）
     * @return
     */
    @GetMapping("/pipelineStatistics")
    public ApiResult<Map<String,Map<String,Long>>> pipelineStatistics() {

        Map<String,Map<String,Long>>  map  = drainPipeService.pipelineStatistics();

        return ApiResult.ok(map);
    }


}
